package com.stylefeng.guns.modular.pay.payment.allin.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.ConnectException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.stylefeng.guns.modular.pay.model.AllinMch;
import com.stylefeng.guns.modular.pay.payment.allin.common.api.MchConfig;
import com.stylefeng.guns.modular.pay.payment.allin.common.core.XmlTools;
import com.stylefeng.guns.modular.pay.payment.allin.common.core.config.Configuer;
import com.stylefeng.guns.modular.pay.payment.allin.common.entity.common.InfoReq;

public class AllinPayUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(AllinPayUtils.class);

	/**
	 * 表头封装
	 * @param trxcod
	 * @return
	 */
	public static InfoReq makeReq(String trxcod,AllinMch config,String reqSn) {
		InfoReq info = new InfoReq();
		info.setTRX_CODE(trxcod);
		if(!Strings.isNullOrEmpty(reqSn)){
			info.setREQ_SN(reqSn);	
		}else{
			info.setREQ_SN(String.valueOf(System.currentTimeMillis()));
		}
		info.setUSER_NAME(config.getUserName());
		info.setUSER_PASS(config.getPassword());
		info.setLEVEL("5");
		info.setDATA_TYPE("2");
		info.setVERSION("03");
		if ("300000".equals(trxcod) || "300001".equals(trxcod) || "300003".equals(trxcod) || "REFUND".equals(trxcod)) {
			info.setMERCHANT_ID(config.getMerchantId());
		}
		return info;
	}
	
	/**
	 * 请求
	 * @param xml
	 * @param url
	 * @param isFront
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws Exception
	 */
	public static String sendXml(String xml,String url,String certPath,boolean isFront) throws UnsupportedEncodingException, Exception{
		LOGGER.info("======================发送报文======================：\n"+xml);
		String resp=XmlTools.send(url,xml);
		LOGGER.info("======================响应内容======================") ;
		boolean flag= verifyMsg(resp,certPath,isFront);
		if(flag){
			LOGGER.info("响应内容验证通过");
		}else{
			LOGGER.info("响应内容验证不通过");
		}
		return resp;
	}
	
	/**
	 * 验签
	 * @param msg
	 * @param cer
	 * @param isFront
	 * @return
	 * @throws Exception
	 */
	public static boolean verifyMsg(String msg,String cer,boolean isFront) throws Exception{
		 boolean flag=XmlTools.verifySign(msg, cer, false,isFront);
		System.out.println("验签结果["+flag+"]") ;
		return flag;
	}
	
	/**
	 * 请求通联
	 * @param xml
	 * @param flag
	 * @param url
	 * @return
	 */
	public static String sendToTlt(String xml,boolean flag,AllinMch config,String url) {
		try{
			if(!flag){
				xml= signMsg(xml,config);
			}else{
				xml=xml.replaceAll("<SIGNED_MSG></SIGNED_MSG>", "");
			}
			return sendXml(xml,url,config.getCerPath(),flag);
		}catch(Exception e){
			e.printStackTrace();
			if(e.getCause() instanceof ConnectException||e instanceof ConnectException){
				System.out.println("请求链接中断，如果是支付请求，请做交易结果查询，以确认该笔交易是否已被通联受理，避免重复交易");
			}
		}
		return "请求链接中断，如果是支付请求，请做交易结果查询，以确认该笔交易是否已被通联受理，避免重复交易";
	}	
	
	
	public static String signMsg(String xml,AllinMch config) throws Exception{
		xml=XmlTools.signMsg(xml, config.getPfxPath(),config.getPfxPassword(), false);
		return xml;
	}
}
